import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'

import {checktoken} from '../apis/users'

Vue.use(VueRouter)

const routes = [{
    path: '/home',
    name: 'Home',
    component: Home,
    children: [{
      path: '/home/accont-add',
      name: 'accont-add',
      component: () => import('../views/modules/account/AccountAdd.vue')
    }, {
      path: '/home/accont-list',
      name: 'accont-list',
      component: () => import('../views/modules/account/AccountList.vue')
    }, {
      path: '/home/change-pwd',
      name: 'change-pwd',
      component: () => import('../views/modules/account/ChangePwd.vue')
    }, {
      path: '/home/shop-manager',
      name: 'shop-manage',
      component: () => import('../views/modules/ShopManager.vue')
    }, {
      path: '/home/home-content',
      name: 'home-content',
      component: () => import('../views/modules/HomeContent.vue')
    }, {
      path: '/home/goods-add',
      name: 'goods-add',
      component: () => import('../views/modules/goods/GoodsAdd.vue')
    }, {
      path: '/home/goods-list',
      name: 'goods-list',
      component: () => import('../views/modules/goods/GoodsList.vue')
    }, {
      path: '/home/goods-class',
      name: 'goods-class',
      component: () => import('../views/modules/goods/GoodsClass.vue')
    }, {
      path: '/home/order-manager',
      name: 'order-manager',
      component: () => import('../views/modules/OrderManager.vue')
    }, {
      path: '/home/goods-total',
      name: 'goods-total',
      component: () => import('../views/modules/total/GoodsTotal.vue')
    }, {
      path: '/home/order-total',
      name: 'order-total',
      component: () => import('../views/modules/total/OrderTotal.vue')
    }, {
      path: '/home/personal',
      name: 'personal',
      component: () => import('../views/modules/account/Personal.vue')
    }]
  },
  {
    path: '/',
    name: 'Login',
    component: Login,
  },
  {
    path: '*',
    name: 'Error',
    component: ()=> import('../views/Error404.vue'),
  },
]

const router = new VueRouter({
  routes
})

//配置全局守卫
router.beforeEach((to, from, next) => {

  if (to.path == '/') {
    next()
  } else {
    checktoken({token:localStorage.token}).then(res=>{
      if(res.data.code===0){
        next()
      }else{
        next('/')
      }
    })
  }
})

export default router